<?php

namespace App\Http\Controllers\Account;

use Illuminate\Support\Facades\Validator;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Account\UserPermissionCategory;
use App\Helpers\Code;

class UserPermissionCategoryController extends Controller
{

    public function __construct()
    {
        $this->rules = [
            'title' => 'bail|required|unique:user_permission_categories|max:50',
            'description' => 'max:100'
        ];

        $this->messages = [
            'title.required' => trans('permission.category.title_required'),
            'title.unique' => trans('permission.category.title_unique'),
            'title.max' => trans('permission.category.title_max'),
            'description.max' => trans('permission.category.description_max')
        ];

        $this->model = new UserPermissionCategory();
    }

    public function addCategory(Request $request)
    {
        try {
            $data = $request->only($this->model->getFillable());

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $this->result['data'] = $this->model->addCategory($data);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function deleteCategory($id)
    {
        try {
            $model = $this->model->deleteCategory($id);

            $this->checked($model, [
                'null' => 'common.delete_failure',
                Code::INSIDE_EXIST_DEPENDENCE => 'permission.category.delete_failure_dependence'
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function editCategory($id, Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['data_scope']));

            $this->rules['title'] = ['bail', 'required', 'max:50', \Illuminate\Validation\Rule::unique('user_permission_categories')->ignore($id)];

            $validator = Validator::make($data, $this->rules, $this->messages);

            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editCategory($id, $data);

            $this->checked($model, ['null' => 'common.edit_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getCategoryList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int) $request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            return preprocess($this->model->getCategoryList($limit, $sorter, $search));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getPermissionListAll()
    {
        try {
            $this->result['data'] = $this->model->getPermissionListAll();
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getCategoryListAll()
    {
        try {
            $this->result['data'] = $this->model->getCategoryListAll();
            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getCategoryPermissionList($id)
    {
        try {
            $model = $this->model->getCategoryPermissionList($id);

            if (!$model) {
                return $this->error('common.get_failure');
            } else {
                return preprocess($model);
            }
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function getCategory($id)
    {
        try {
            $model = $this->model->getCategory($id);

            $this->checked($model, ['null' => 'common.get_failure']);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

}
